package com.demo.controller;

import com.alibaba.fastjson.JSON;
import com.demo.model.*;
import com.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private User2Service user2Service;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderExpandService orderExpandService;
    @Autowired
    private ProductService productService;

    @RequestMapping("/product/list.htm")
    @ResponseBody
    public String list(String name, Model model) {
        List<Product> products = productService.getProducts(null);
        model.addAttribute("products", products);
        model.addAttribute("name", name);
        return JSON.toJSONString(products);
    }

    @RequestMapping("/order/list.htm")
    @ResponseBody
    public String list(Integer id, Model model) {
        List<Order> orders = orderService.getOrders(null);
        model.addAttribute("orders", orders);
        model.addAttribute("id", id);
        return JSON.toJSONString(orders);
    }

    @RequestMapping("/addProduct/list.htm")
    @ResponseBody
    public String addProduct(int number, int productId, int userId) {
        ShoppingCart shoppingCart = new ShoppingCart();
        if (productId > 0 && userId > 0) {
            Product product = productService.getProductById(productId);
            if (number <= product.getStock() && number > 0) {
                shoppingCart.setUserId(userId);
                shoppingCart.setProductId(productId);
                shoppingCart.setPrice(product.getPrice());
                shoppingCart.setNumber(number);
                shoppingCart.setTotal(number * product.getPrice());
                shoppingCartService.save(shoppingCart);
            } else {
                System.out.println("下单失败,数量有误");
            }
        } else {
            System.out.println("id输入有误");
        }
        return JSON.toJSONString(shoppingCart);
    }

    @RequestMapping("/selectOrdersByUserId/list.htm")
    @ResponseBody
    public String selectOrdersByUserId(int userId) {
        ProductBo productBo = new ProductBo();
        List<ProductBo> list = new ArrayList();
        if (userId > 0) {
            User2 user2 = user2Service.getUser2ById(userId);
            List<Order> orders = orderService.getOrderByUserId(userId);
            for (int i = 0; i < orders.size(); i++) {
                Order order = orders.get(i);
                OrderExpand orderExpand = orderExpandService.getOrderExpandByOrderId(order.getId());
                if (orderExpand != null) {
                    Product product = productService.getProductById(orderExpand.getProductId());
                    productBo.setUsername(user2.getUsername());
                    productBo.setName(product.getName());
                    productBo.setPrice(orderExpand.getPrice());
                    productBo.setNumber(orderExpand.getNumber());
                    productBo.setCount(order.getTotal());
                    list.add(productBo);
                }
            }
        } else {
            System.out.println("ID有误");
        }
        return JSON.toJSONString(list);
    }

    @RequestMapping("/placeOrder/list.htm")
    @ResponseBody
    public String placeOrder(int userId) {
        Order order = new Order();
        order.setUserId(userId);
        order.setPayStatus(1);
        order.setTotal(0.0);
        orderService.save(order);
        List<OrderExpand> orderExpandList = new ArrayList<>();
        OrderExpand orderExpand = new OrderExpand();
        List<ShoppingCart> shoppingCartList = shoppingCartService.getShoppingCartByUserId(userId);
        for (int i = 0; i < shoppingCartList.size(); i++) {
            ShoppingCart shoppingCart = shoppingCartList.get(i);
            orderExpand.setOrderId(order.getId());
            orderExpand.setProductId(shoppingCart.getProductId());
            orderExpand.setPrice(shoppingCart.getPrice());
            orderExpand.setNumber(shoppingCart.getNumber());
            orderExpandList.add(orderExpand);
            Product product =productService.getProductById(shoppingCart.getProductId());
            product.setStock(product.getStock()-shoppingCart.getNumber());
            orderExpandService.save(orderExpand);
            order.setTotal(order.getTotal() + orderExpand.getPrice() * orderExpand.getNumber());
            orderService.updateOrder(order);
            productService.updateProduct(product);
            shoppingCartService.deleteShoppingCart(shoppingCart.getId());
        }
        return JSON.toJSONString(order);
    }
}

